<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>echarts地图</title>
    <script src="../js/data.js"></script>
    <script src="../js/echarts.min.js"></script>
    <script src="../js/jquery.min.js"></script>
</head>

<body>
<div class="viewport" id="home">
    <div class="echarts" id="echarts"></div>
</div>
</body>
<script>
    // 获取数据
    function getData() {
        $.get('../js/data.json', function (data) {
            getEcharts(data)
        });
    }

    // 渲染echarts
    function getEcharts(data) {
        var chartDom = document.getElementById('echarts');
        var myChart = echarts.init(chartDom);
        var nameMap = '地图数据';
        var mapData = []
        echarts.registerMap(nameMap, data);
        /*获取地图数据*/
        myChart.showLoading();
        var mapFeatures = echarts.getMap(nameMap).geoJson.features;
        myChart.hideLoading();
        mapFeatures.forEach(function (v, index) {
            // 地区名称
            mapData.push({
                name: v.properties.name,
                value: Math.random() * 100
            });
        });
        var serverdata = [
            { // 地图块的相关信息
                type: 'map',
                map: nameMap,
                zoom: 1.2,
                roam: false,
                selectedMode: "single",
                showLegendSymbol: false,
                visibility: "off",
                label: {
                    show: false,
                    normal: {
                        show: true,
                        textStyle: {
                            fontSize: 12,
                            fontWeight: 400,
                            color: '#fff'
                        }
                    }
                },
                data: mapData
            }
        ]
        var optionMap = {
            toolbox: {
                show: true,
                feature: {
                    saveAsImage: {
                        show: true,
                        excludeComponents: ['toolbox'],
                        pixelRatio: 1
                    }
                }
            },
            visualMap: {
                type: "piecewise",
                pieces: [
                    {max: 30, label: "安全", color: "#2c9a42"},
                    {min: 30, max: 60, label: "警告", color: "#d08a00"},
                    {min: 60, label: "危险", color: "#c23c33"},
                ],
                color: "#fff",
                textStyle: {
                    color: "#fff",
                },
                visibility: "off",
            },
            geo: {
                map: nameMap,
                show: false,
                roam: true,
                label: {
                    normal: {
                        show: true
                    },
                    emphasis: {
                        show: false
                    }
                }
            },
            series: serverdata
        };
        myChart.setOption(optionMap);

    }

    if (window.location.origin == 'file://') {
        setTimeout(() => {
            getEcharts(gdMap)
        }, 100)
    } else {
        getData()
    }
</script>
</html>
<style>
    html, body {
        padding: 0;
        margin: 0;
    }

    #home {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .echarts {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .info {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;
        color: red;
    }

    .links {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;

    }

    .links a {
        text-decoration: none;
        color: #fff;
    }
</style>
